<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>生成数据库表设计文档</title>
<link
	href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.0.2/css/bootstrap.min.css"
	rel="stylesheet">
<link href="https://cdn.bootcdn.net/ajax/libs/bootstrap-switch/3.3.4/css/bootstrap2/bootstrap-switch.min.css" rel="stylesheet">
<script
	src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script
	src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.0.2/js/bootstrap.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/bootstrap-switch/3.3.4/js/bootstrap-switch.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery-validate/1.19.3/jquery.validate.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery-validate/1.19.3/localization/messages_zh.min.js"></script>
<style>
	.input-group-text{
		width: 100px;
		justify-content: center;
	}
	.toastdiv{
		position: absolute;
	    top: 35%;
	    left: 40%;
	}
	.error{
		color:red;
	}
	.bootstrap-switch-top{
		top: 14px;
	}
</style>
</head>
<body>
	<div class="container">
		<h1 class="text-center p-3">生成数据库表设计文档</h1>
		<div class="container overflow-hidden p-3">
			<div class="row gy-5">
				<div class="col-4">
					<div class="p-3" style="text-align: right;">当前数据库</div>
				</div>
				<div class="col-2">
					<div class="p-3 border bg-light">
						<span id="currentDB"></span>
					</div>
				</div>
				<!-- <div class="col-2">
					<input type="checkbox" name="isTableHeadCenter" value="false"/>
				</div> -->
				<div class="col-4 p-2">
					<button type="button" class="btn btn-primary"
						onclick="downloaddoc()">立即下载</button>
				</div>
			</div>
		</div>
		<div class="container overflow-hidden p-3 border">
			<h2 class="text-center p-3">变更数据库</h2>
			<form id="changeForm" action="" method="get">
				<div class="input-group mb-3">
					<span class="input-group-text">数据库地址</span>
					<input type="text" class="form-control" id="url" name="url" required>
				</div>
				<div class="input-group mb-3">
					<span class="input-group-text">用户名</span>
					<input type="text" class="form-control" id="username" name="username" required>
				</div>
				<div class="input-group mb-3">
					<span class="input-group-text">密码</span>
					<input type="password" class="form-control" id="password" name="password" required>
				</div>
				<button type="submit" class="btn btn-primary">更变</button>
			</form>
		</div>
	</div>
	<div class="toastdiv">
		<div aria-live="polite" aria-atomic="true"
			class="d-flex justify-content-center align-items-center w-100">
			<div id="toast" class="toast" role="alert" aria-live="assertive"
				aria-atomic="true">
				<div class="toast-header">
					<strong class="me-auto">提示</strong>
				</div>
				<div class="toast-body">数据库切换中，请稍候……</div>
			</div>
		</div>
	</div>
</body>
<script>
	$.validator.setDefaults({
	    submitHandler: function() {
	    	$(".container:first").css("opacity", "0.4");
			$(".toastdiv").css("opacity", "1");
			$("button,input").attr("disabled", "disabled");
			$("#toast").toast("show");
			$.post("./change", {
				"url" : $("#url").val(),
				"username" : $("#username").val(),
				"password" : window.btoa($("#password").val())
			}, function() {
				var limit = 0;
				var stop = setInterval(function() {
					$.get("./currentDB", function(data) {
						if (limit == 3 || data != $("#currentDB").text()) {
							clearInterval(stop);
							$("#currentDB").text(data);
							$("button,input").removeAttr("disabled");
							$("#url,#username,#password").val("");
							$("#toast").toast("hide");
							$(".container:first,.toastdiv").removeAttr("style");
						}
						limit++;
					});
				}, 1200);
			});
	    }
	});
	$(function() {
		getCurrentDB();
		$("input[name='isTableHeadCenter']").bootstrapSwitch({  
            onText : "表头居中",
            offText : "表头靠左",
            onColor : "info",//info/success/warning/danger/primary  
            offColor : "primary",
            size : "normal",//mini/small/normal/large
            handleWidth:"60",//设置控件宽度
            wrapperClass: "top",
            // 当开关状态改变时触发  
            onSwitchChange : function(event, state) {
            	$(this).val(state);
            }  
        });
		$("#changeForm").validate();
	});
	function getCurrentDB() {
		$.get("./currentDB", function(data) {
			$("#currentDB").text(data);
		});
	}
	function downloaddoc() {
		// var url = "./produce?isTableHeadCenter=" + $("input[name='isTableHeadCenter']").val();
		var url = "./produce";
		var xhr = new XMLHttpRequest();
		xhr.open('GET', url, true);
		xhr.responseType = "blob";
		xhr.onload = function () {
			if (this.status === 200) {
				var blob = this.response;
				var headers = xhr.getAllResponseHeaders();
				var a = document.createElement('a');
				var filename = "文档.docx";
				var arr = headers.trim().split(/[\r\n]+/);
				var headerMap = {};
				arr.forEach(function (line) {
				    var parts = line.split(':')
				    var header = parts.shift()
				    var value = parts.join(':')
				    headerMap[header] = value;
				});
				if(headerMap["content-disposition"] && headerMap["content-disposition"].indexOf("fileName=") != -1){
				    filename = headerMap["content-disposition"].split(";")[1].split("fileName=")[1];
				    filename = decodeURI(filename);
				}
				a.download = filename;
				a.href = window.URL.createObjectURL(blob);
				a.click();
			}
		}
		// 发送ajax请求
		xhr.send();
	}
</script>
</html>